<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 求最小步数（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>求从坐标零点到坐标点n的最小步数&#xff0c;一次只能沿横坐标轴向左或向右移动 2 或 3。</p> 
<p>注意&#xff1a;途径的坐标点可以为负数</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>坐标点n</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出从坐标零点移动到坐标点n的最小步数</p> 
<p></p> 
<h4>备注</h4> 
<p>1 &lt;&#61; n &lt;&#61; 10^9</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">4</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">2</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">从坐标零点移动到4&#xff0c;最小需要两步&#xff0c;即右移2&#xff0c;再右移2</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题可以从简单的小数量级问题种推导出复杂的大数量级问题的规律&#xff1a;</p> 
<p>1  &#61; -2 &#43; 3<br /> 2  &#61; 2<br /> 3  &#61; 3</p> 
<hr /> 
<p>4  &#61; 2 &#43; 2<br /> 5  &#61; 3 &#43; 2<br /> 6  &#61; 3 &#43; 3</p> 
<hr /> 
<p>7  &#61; 3 &#43; 2 &#43; 2<br /> 8  &#61; 3 &#43; 3 &#43; 2<br /> 9  &#61; 3 &#43; 3 &#43; 3</p> 
<hr /> 
<p>10 &#61; 3 &#43; 3 &#43; 2 &#43; 2<br /> 11 &#61; 3 &#43; 3 &#43; 3 &#43; 2<br /> 12 &#61; 3 &#43; 3 &#43; 3 &#43; 3</p> 
<hr /> 
<p>13 &#61; 3 &#43; 3 &#43; 3 &#43; 2 &#43; 2<br /> 14 &#61; 3 &#43; 3 &#43; 3 &#43; 3 &#43; 2<br /> 15 &#61; 3 &#43; 3 &#43; 3 &#43; 3 &#43; 3</p> 
<hr /> 
<p>16 &#61; 3 &#43; 3 &#43; 3 &#43; 3 &#43; 2 &#43; 2</p> 
<p></p> 
<p>从上面推导过程&#xff0c;</p> 
<p>我们可以发现&#xff0c;n&#61;1,2,3时不具备规律&#xff0c;但是当n&gt;&#61;4时&#xff0c;n每增加3&#xff0c;则对应最少步数&#43;1&#xff0c;</p> 
<p>其实原因也很简单&#xff0c;通过上面过程我们可以发现&#xff0c;</p> 
<p>其实n&gt;&#61;4开始&#xff0c;</p> 
<p>如果n的分解数中存在2&#xff0c;那么n&#43;1&#xff0c;其实就是将2变为3&#xff0c;此时最小步数保持不变。</p> 
<blockquote> 
 <p>4  &#61; 2 &#43; 2<br /> 5  &#61; 3 &#43; 2</p> 
</blockquote> 
<p>如果n的分解数种不存在2&#xff0c;那么n&#43;1&#xff0c;其实就是将3变为2&#43;2&#xff0c;此时会产生最小步数&#43;1的效果。</p> 
<blockquote> 
 <p>6  &#61; 3 &#43; 3</p> 
 <p>7  &#61; 3 &#43; 2 &#43; 2</p> 
</blockquote> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  console.log(getResult(parseInt(line)));
});

function getResult(n) {
  if (n &#61;&#61; 1) return 2;
  if (n &#61;&#61; 2) return 1;
  if (n &#61;&#61; 3) return 1;

  let base &#61; 2;
  return Math.floor((n - 4) / 3) &#43; base;
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; sc.nextInt();

    System.out.println(getResult(n));
  }

  public static int getResult(int n) {
    if (n &#61;&#61; 1) return 2; // -2 &#43; 3
    if (n &#61;&#61; 2) return 1; // 2
    if (n &#61;&#61; 3) return 1; // 3

    int base &#61; 2;
    return (n - 4) / 3 &#43; base;
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())


# 算法入口
def getResult():
    if n &#61;&#61; 1:
        return 2

    if n &#61;&#61; 2:
        return 1

    if n &#61;&#61; 3:
        return 1

    base &#61; 2
    return (n - 4) // 3 &#43; base


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

int main()
{
	int n;
	scanf(&#34;%d&#34;, &amp;n);
	
	int ans;
	
	switch(n) {
		case 1:
			ans &#61; 2;
			break;
		case 2:
			ans &#61; 1;
			break;
		case 3:
			ans &#61; 1;
			break;
		default:
			ans &#61; (n - 4) / 3 &#43; 2;
	}
	
	printf(&#34;%d\n&#34;, ans);
	
	return 0;
}</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>